package com.hiwhitley.himei.module.video;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.hiwhitley.himei.R;
import com.hiwhitley.himei.contract.DailyContract;
import com.hiwhitley.himei.contract.model.DailyModel;
import com.hiwhitley.himei.contract.presenter.DailyPresenter;
import com.hiwhitley.himei.entity.Video;
import com.hiwhitley.himei.mvpframe.base.BaseFrameFragment;
import com.hiwhitley.himei.network.HttpAdress;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import me.drakeet.multitype.FlatTypeClassAdapter;
import me.drakeet.multitype.MultiTypeAdapter;

/**
 * Created by hiwhitley on 2016/9/24.
 */

public class DailyFragment extends BaseFrameFragment<DailyPresenter, DailyModel> implements DailyContract.View {

    @BindView(R.id.rv_daily)
    RecyclerView mRvDaily;

    private static final String TAG = "DailyFragment";
    @BindView(R.id.refresh_layout)
    TwinklingRefreshLayout mRefreshLayout;
    private MultiTypeAdapter mMultiTypeAdapter;
    private List<Video.IssueListBean.ItemListBean> mItemListBean;
    private String nextUrl;

    public static DailyFragment newInstance() {
        return new DailyFragment();
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_daily;
    }

    @Override
    protected void init() {

        mPresenter.loadDaily(HttpAdress.DAILY);

    }

    @Override
    protected void setListener() {
        mItemListBean = new ArrayList<>();

        mMultiTypeAdapter = new MultiTypeAdapter();

        mMultiTypeAdapter.setFlatTypeAdapter(new FlatTypeClassAdapter() {
            @NonNull
            @Override public Class onFlattenClass(@NonNull Object item) {
                return ((Video.IssueListBean.ItemListBean) item).typeClass;
            }
        });

        mMultiTypeAdapter.register(Video.IssueListBean.ItemListBean.VideoType.class, new VideoTypeViewProvider());
        mMultiTypeAdapter.register(Video.IssueListBean.ItemListBean.TextType.class, new TextTypeViewProvider());
        mRvDaily.setLayoutManager(new LinearLayoutManager(mActivity));
        mRvDaily.setAdapter(mMultiTypeAdapter);

        mRefreshLayout.setEnableRefresh(false);
        mRefreshLayout.setOverScrollRefreshShow(false);
        mRefreshLayout.setOverScrollBottomShow(true);
        mRefreshLayout.setAutoLoadMore(true);
        mRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                (new android.os.Handler()).postDelayed(() -> {
                    mPresenter.loadDaily(nextUrl);
                    refreshLayout.finishLoadmore();
                }, 1200);
            }
        });

    }

    @Override
    public void showDaily(List<Video.IssueListBean.ItemListBean> beanList, String nextUrl) {
        Log.d(TAG, "showDaily" + beanList.size());
        this.nextUrl = nextUrl;
        for (Video.IssueListBean.ItemListBean itemListBean : beanList) {
            Log.d(TAG, "showDaily" + itemListBean.getType());
            itemListBean.typeClass = Video.IssueListBean.ItemListBean.getTypeClass(itemListBean.getType());
        }

        mItemListBean.addAll(beanList);
        mMultiTypeAdapter.setItems(mItemListBean);
        mMultiTypeAdapter.notifyDataSetChanged();
    }

    @Override
    public void onRequestStart() {

    }

    @Override
    public void onRequestEnd() {

    }
}
